home *** CD-ROM | disk | FTP | other *** search
/ Millennium Gold 2000 / Millennium Gold 2000 - Disc 1.iso / GNUCHESS / BOOK.C < prev    next >
C/C++ Source or Header  |  1995-07-27  |  6KB  |  206 lines

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30
  5.  
  6.   Modified by Daryl Baker for use in MS WINDOWS environment
  7.  
  8.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  9.   Copyright (c) 1988, 1989, 1990  John Stanback
  10.  
  11.   This file is part of CHESS.
  12.  
  13.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  14.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  15.   the consequences of using it or for whether it serves any particular
  16.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  17.   General Public License for full details.
  18.  
  19.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  20.   only under the conditions described in the CHESS General Public License.
  21.   A copy of this license is supposed to have been given to you along with
  22.   CHESS so you can know your rights and responsibilities.  It should be in a
  23.   file named COPYING.  Among other things, the copyright notice and this
  24.   notice must be preserved on all copies.
  25. */
  26.  
  27. #define NOATOM 
  28. #define NOCLIPBOARD
  29. #define NOCREATESTRUCT
  30. #define NOFONT
  31. #define NOREGION
  32. #define NOSOUND
  33. #define NOWH
  34. #define NOCOMM
  35. #define NOKANJI
  36.  
  37. #include <windows.h>
  38. #include <time.h>
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41.  
  42. #include "gnuchess.h"
  43. #include "defs.h"
  44. #include "chess.h"
  45.  
  46. #define MAX_BOOK_SIZE (32*1024)
  47.  
  48. static unsigned int book_used = 0;
  49. static char far * xBook;
  50. GLOBALHANDLE hBook = 0;
  51.  
  52. void FreeBook (void)
  53. {
  54.    GlobalUnlock ( hBook );
  55.    GlobalFree ( hBook);
  56.    hBook = 0;
  57.    book_used = 0;
  58. }
  59.  
  60. static void far *Book_alloc ( unsigned int size )
  61. {
  62.     char far * temp;
  63.     if ( book_used+size >= MAX_BOOK_SIZE ) return (void far *)0;
  64.     temp = xBook+book_used;
  65.     book_used += size;
  66.     return temp;
  67. }
  68.  
  69.  
  70. void
  71. GetOpenings (HWND hWnd)
  72.      
  73. /*
  74.    Read in the Opening Book file and parse the algebraic notation for a move
  75.    into an unsigned integer format indicating the from and to square. Create
  76.    a linked list of opening lines of play, with entry->next pointing to the
  77.    next line and entry->move pointing to a chunk of memory containing the
  78.    moves. More Opening lines of up to 256 half moves may be added to
  79.    gnuchess.book.
  80. */
  81. #ifndef BOOK
  82. #define BOOK "/usr/games/lib/gnuchess.book"
  83. #endif /* BOOK */     
  84. {
  85.   FILE *fd;
  86.   int c, i, j, side;
  87.  
  88.   /* char buffr[2048]; */
  89.   struct BookEntry far  *entry;
  90.   unsigned short mv, far  *mp, tmp[100];
  91.   char lpFile[_MAX_FNAME+_MAX_EXT+_MAX_DRIVE+_MAX_DIR+1];
  92.   char sFname[_MAX_FNAME], sExt[_MAX_EXT], sDrive[_MAX_DRIVE], sDir[_MAX_DIR];
  93.  
  94.  
  95. #ifdef WIN32
  96.   GetModuleFileName ( (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
  97.                       lpFile, sizeof(lpFile) );
  98. #else
  99.   GetModuleFileName ( GetWindowWord(hWnd, GWW_HINSTANCE),
  100.                       lpFile, sizeof(lpFile) );
  101. #endif
  102.  
  103.   _splitpath ( lpFile, sDrive, sDir, sFname, sExt);
  104.   _makepath  ( lpFile, sDrive, sDir, "gnuchess", "boo");
  105.  
  106.   fd = fopen (lpFile, "r");
  107.  
  108. /*  if ((fd = fopen (BOOK, "r")) == NULL)
  109.     fd = fopen ("gnuchess.book", "r"); */
  110.   if (fd != NULL)
  111.     {
  112.       hBook = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  113.                             (long) (MAX_BOOK_SIZE * sizeof (char)) );
  114.  
  115.       if( hBook == NULL )
  116.         {
  117.            Book = NULL;
  118.            SMessageBox (hWnd, IDS_OBAE, IDS_CHESS);
  119.            return;
  120.         }
  121.       xBook = (unsigned char far *) GlobalLock (hBook);
  122.  
  123.       Book = NULL;
  124.       i = 0;
  125.       side = white;
  126.       while ((c = parse (fd, &mv, side)) >= 0)
  127.         if (c == 1)
  128.           {
  129.             tmp[++i] = mv;
  130.             side = otherside[side];
  131.           }
  132.         else if (c == 0 && i > 0)
  133.           {
  134.             entry = (struct BookEntry far *) Book_alloc ( sizeof (struct BookEntry));
  135.             mp = (unsigned short far *) Book_alloc ( (i + 1) * sizeof (unsigned short));
  136.             if ( (entry == 0 ) || (mp == 0) )
  137.               {
  138.                 Book = NULL;
  139.                 SMessageBox (hWnd, IDS_OBAE, IDS_CHESS);
  140.                 GlobalUnlock ( hBook );
  141.                 GlobalFree ( hBook);
  142.                 return;
  143.               }
  144.             entry->mv = mp;
  145.             entry->next = Book;
  146.             Book = entry;
  147.             for (j = 1; j <= i; j++)
  148.               *(mp++) = tmp[j];
  149.             *mp = 0;
  150.             i = 0;
  151.             side = white;
  152.           }
  153.  
  154.       fclose (fd);
  155.     }
  156.   else
  157.     SMessageBox (hWnd, IDS_OBNF, IDS_CHESS);
  158. }
  159.  
  160. void
  161. OpeningBook (unsigned short *hint)
  162.  
  163. /*
  164.   Go thru each of the opening lines of play and check for a match with the
  165.   current game listing. If a match occurs, generate a random number. If this
  166.   number is the largest generated so far then the next move in this line
  167.   becomes the current "candidate". After all lines are checked, the
  168.   candidate move is put at the top of the Tree[] array and will be played by
  169.   the program. Note that the program does not handle book transpositions.
  170. */
  171.  
  172. {
  173.   short j, pnt;
  174.   unsigned short m, far *mp;
  175.   unsigned r, r0;
  176.   struct BookEntry far *p;
  177.  
  178.   srand ((unsigned int) time ((long *) 0));
  179.   r0 = m = 0;
  180.   p = Book;
  181.   while (p != NULL)
  182.     {
  183.       mp = p->mv;
  184.       for (j = 1; j <= GameCnt; j++)
  185.         if (GameList[j].gmove != *(mp++))
  186.           break;
  187.       if (j > GameCnt)
  188.         if ((r = urand ()) > r0)
  189.           {
  190.             r0 = r;
  191.             m = *mp;
  192.             *hint = *(++mp);
  193.           }
  194.       p = p->next;
  195.     }
  196.  
  197.   for (pnt = TrPnt[1]; pnt < TrPnt[2]; pnt++)
  198.     if (((Tree[pnt].f << 8) | Tree[pnt].t) == m)
  199.       Tree[pnt].score = 0;
  200.   pick (TrPnt[1], TrPnt[2] - 1);
  201.   if (Tree[TrPnt[1]].score < 0) {
  202.    FreeBook ();
  203.    Book = NULL;
  204.   }
  205. }
  206.